﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class LayEquipmentRadiator : HYExternalCommand
	{
		public override Result HYExecute(ref string TranName)
		{
			TranName = "散热器自动布置";
			RadiatorForm radiatorForm = new RadiatorForm(base.Doc);
			DialogResult dialogResult;
			for (dialogResult = radiatorForm.ShowDialog(); dialogResult == DialogResult.Retry; dialogResult = radiatorForm.ShowDialog())
			{
				try
				{
					if (radiatorForm.IsCurrentDoc)
					{
						ISelectionFilter selectionFilter = new SpaceSelectionFilter();
						radiatorForm.lisSpaceSelected = base.Sel.PickElementsByRectangle(selectionFilter, "请选择需要布置散热器的空间...").ToList<Element>();
						if (radiatorForm.lisSpaceSelected.Count > 0)
						{
							radiatorForm.spaceSelected = radiatorForm.lisSpaceSelected.FirstOrDefault<Element>();
						}
					}
					else if (!this.GetRevitLinkInstanceSpace(radiatorForm))
					{
						YJKMessageBox.Information("未选中Space！");
						return Autodesk.Revit.UI.Result.Cancelled;
					}
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
				}
				catch (Exception ex)
				{
					YJKMessageBox.Show(ex.Message);
				}
			}
			while (dialogResult == DialogResult.OK)
			{
				try
				{
					LayEquipmentRadiator.RadiatorLayParameter rlp = new LayEquipmentRadiator.RadiatorLayParameter();
					string layType = radiatorForm.raly.LayType;
					if (!(layType == "1"))
					{
						if (!(layType == "2"))
						{
							if (layType == "0")
							{
								foreach (Element spaceSelected in radiatorForm.lisSpaceSelected)
								{
									radiatorForm.spaceSelected = spaceSelected;
									this.LayWindowAndWall(rlp, radiatorForm);
								}
								return Autodesk.Revit.UI.Result.Succeeded;
							}
						}
						else if (!this.LayOnWall(rlp, radiatorForm))
						{
							this.message = "请选择墙！";
							return Autodesk.Revit.UI.Result.Cancelled;
						}
					}
					else
					{
						this.LayFree(rlp, radiatorForm);
					}
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
				}
				catch (Exception ex2)
				{
					ex2.ToString();
				}
				radiatorForm = new RadiatorForm(base.Doc);
				dialogResult = radiatorForm.ShowDialog();
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void LayWindowAndWall(LayEquipmentRadiator.RadiatorLayParameter rlp, RadiatorForm rf)
		{
			List<Element> list = YJKRevitTools.GetDoorsOrWindowsInSpaceOrRoom(rf.spaceSelected, BuiltInCategory.OST_Windows);
			List<Element> list2 = YJKRevitTools.GetDoorsOrWindowsInSpaceOrRoom(rf.spaceSelected, BuiltInCategory.OST_Doors);
			if ((list2 == null || list2.Count == 0) && (list == null || list.Count == 0))
			{
				if (list == null)
				{
					list = new List<Element>();
				}
				if (list2 == null)
				{
					list2 = new List<Element>();
				}
				list2.AddRange(YJKRevitTools.GetSapceElements(rf.spaceSelected, BuiltInCategory.OST_Doors));
				list.AddRange(YJKRevitTools.GetSapceElements(rf.spaceSelected, BuiltInCategory.OST_Windows));
			}
			if ((list != null && list.Count > 0) || (list2 != null && list2.Count > 0))
			{
				if (list != null && list.Count > 0)
				{
					using (List<Element>.Enumerator enumerator = list.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Element window = enumerator.Current;
							rlp.Window = window;
							this.GetLayParameter(ref rlp, rf.raly, rf.spaceSelected);
							rlp.RadiatorHeatLoad /= (double)list.Count;
							this.LayRadiator(rf, rlp, rf.IsSetNum, rf.Num, rf.IsDimension);
						}
						return;
					}
				}
				rlp.Window = list2[0];
				this.GetLayParameter(ref rlp, rf.raly, rf.spaceSelected);
				this.LayRadiator(rf, rlp, rf.IsSetNum, rf.Num, rf.IsDimension);
				return;
			}
			YJKMessageBox.Information("没有找到门或窗，或者门窗在链接文档中，请选择散热器的布置点");
			XYZ pickPoint = base.Sel.PickPoint((Autodesk.Revit.UI.Selection.ObjectSnapTypes)4, "请选择散热器的布置位置");
			rlp.LocationLay = this.GetRadiatorLayPoint(pickPoint, rf.spaceSelected, ref rlp.WindowFacingOrientation);
			rlp.LocationLay -= rlp.WindowFacingOrientation * (rf.raly.WallDistance / 304.8);
			rlp.LayType = 1;
			this.LayRadiator(rf, rlp, false, 0, false);
		}

		private bool LayOnWall(LayEquipmentRadiator.RadiatorLayParameter rlp, RadiatorForm rf)
		{
			XYZ xyz = new XYZ();
			Curve curve = null;
			Wall wall = this.SelectWall(ref xyz, ref curve);
			if (wall == null)
			{
				return false;
			}
			Curve curve2 = (wall.Location as LocationCurve).Curve;
			rlp.LocationLay = new XYZ(xyz.X, xyz.Y, curve2.GetEndPoint(0).Z);
			XYZ xyz2 = curve2.Project(rlp.LocationLay).XYZPoint;
			rlp.WindowFacingOrientation = (rlp.LocationLay.IsAlmostEqualTo(xyz2) ? (-wall.Orientation) : (-(rlp.LocationLay - xyz2).Normalize()));
			if (wall.Document.IsLinked)
			{
				xyz2 = base.UiDoc.Document.GetLinkedDocument(wall.Id).GetPointInLinkDocument(xyz2);
			}
			rlp.LocationLay = xyz2;
			rlp.LocationLay -= rlp.WindowFacingOrientation * (0.5 * wall.Width + rf.raly.WallDistance / 304.8);
			rlp.LayType = 1;
			this.LayRadiator(rf, rlp, rf.IsSetNum, rf.Num, rf.IsDimension);
			return true;
		}

		private void LayFree(LayEquipmentRadiator.RadiatorLayParameter rlp, RadiatorForm rf)
		{
			XYZ xyz;
			using (LayRadiatorDrawJin layRadiatorDrawJin = new LayRadiatorDrawJin(base.UiDoc, true))
			{
				layRadiatorDrawJin.StartPoint = new XYZ();
				xyz = base.Sel.PickPoint("请选择散热器的布置位置...");
				layRadiatorDrawJin.Dispose();
			}
			XYZ xyz2;
			using (LayRadiatorDrawJinPolarAxis layRadiatorDrawJinPolarAxis = new LayRadiatorDrawJinPolarAxis(base.UiDoc))
			{
				layRadiatorDrawJinPolarAxis.StartPoint = xyz;
				Application.DoEvents();
				xyz2 = layRadiatorDrawJinPolarAxis.PickEndPoint(xyz, "请选择散热器方向...");
				layRadiatorDrawJinPolarAxis.Dispose();
			}
			rlp.LocationLay = xyz;
			rlp.WindowFacingOrientation = (xyz2 - xyz).Normalize();
			rlp.LayType = 1;
			this.LayRadiator(rf, rlp, rf.IsSetNum, rf.Num, rf.IsDimension);
		}

		private bool GetRevitLinkInstanceSpace(RadiatorForm rf)
		{
			PickedBox pickedBox = base.Sel.PickBox((Autodesk.Revit.UI.Selection.PickBoxStyle)1, "请选择需要布置散热器的空间...");
			Outline outlineByMinAndMaxPoint = this.GetOutlineByMinAndMaxPoint(base.Doc, pickedBox.Min, pickedBox.Max, base.Doc.ActiveView.GenLevel.Elevation);
			if (outlineByMinAndMaxPoint == null || outlineByMinAndMaxPoint.IsEmpty)
			{
				return false;
			}
			List<Element> list = new List<Element>();
			List<Element> list2 = new FilteredElementCollector(base.Doc, base.Doc.ActiveView.Id).OfClass(typeof(RevitLinkInstance)).ToList<Element>();
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_MEPSpaces);
			foreach (Element element in list2)
			{
				RevitLinkInstance revitLinkInstance = (RevitLinkInstance)element;
				List<Space> list3 = new FilteredElementCollector(revitLinkInstance.GetLinkDocument()).WherePasses(elementCategoryFilter).WhereElementIsNotElementType().Cast<Space>().ToList<Space>();
				Transform totalTransform = revitLinkInstance.GetTotalTransform();
				foreach (Space space in list3)
				{
					if (Math.Abs(space.Level.Elevation - base.Doc.ActiveView.GenLevel.Elevation) <= 0.1)
					{
						BoundingBoxXYZ boundingBoxXYZ = space.get_BoundingBox(base.Doc.ActiveView);
						if (outlineByMinAndMaxPoint.Intersects(new Outline(boundingBoxXYZ.Min + totalTransform.Origin, boundingBoxXYZ.Max + totalTransform.Origin), 0.01))
						{
							list.Add(space);
						}
					}
				}
			}
			if (list.Count < 1)
			{
				return false;
			}
			rf.lisSpaceSelected = list;
			rf.spaceSelected = rf.lisSpaceSelected.FirstOrDefault<Element>();
			return true;
		}

		private bool GetRevitLinkInstanceSpaceByPickObjects(RadiatorForm rf)
		{
			IEnumerable<Reference> enumerable = base.Sel.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)5, "请选择需要布置散热器的空间...");
			List<Element> list = new List<Element>();
			foreach (Reference reference in enumerable)
			{
				Element elementById = (base.Doc.GetElement(reference) as RevitLinkInstance).GetLinkDocument().GetElementById(reference.LinkedElementId);
				if (elementById is Space)
				{
					list.Add(elementById);
				}
			}
			if (list.Count < 1)
			{
				return false;
			}
			rf.lisSpaceSelected = list;
			rf.spaceSelected = list.FirstOrDefault<Element>();
			return true;
		}

		private Outline GetOutlineByMinAndMaxPoint(Autodesk.Revit.DB.Document doc, XYZ min, XYZ max, double hight)
		{
			double x;
			double x2;
			if (min.X > max.X)
			{
				x = max.X;
				x2 = min.X;
			}
			else
			{
				x2 = max.X;
				x = min.X;
			}
			double y;
			double y2;
			if (min.Y > max.Y)
			{
				y = max.Y;
				y2 = min.Y;
			}
			else
			{
				y = min.Y;
				y2 = max.Y;
			}
			return new Outline(new XYZ(x, y, hight), new XYZ(x2, y2, hight + 3.2808398950131235));
		}

		private bool GetLinkSpace(RadiatorForm rf)
		{
			Reference reference = base.Sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)5, "请选择需要布置散热器的空间");
			Document linkDocument = (base.Doc.GetElement(reference) as RevitLinkInstance).GetLinkDocument();
			Element ele = linkDocument.GetElementById(reference.LinkedElementId);
			if (ele is Space)
			{
				rf.spaceSelected = (ele as Space);
				return true;
			}
			if (!(ele is Room))
			{
				return false;
			}
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_MEPSpaces);
			Space space = new FilteredElementCollector(ele.Document).WherePasses(elementCategoryFilter).WhereElementIsNotElementType().ToElements().ToList<Element>().Find((Element p) => (p as Space).Room != null && (p as Space).Room.Id.IntegerValue == ele.Id.IntegerValue) as Space;
			if (space != null)
			{
				rf.spaceSelected = space;
				return true;
			}
			return false;
		}

		private Wall GetSelWall(bool isCurrent, ref Reference refer)
		{
			Wall result = null;
			if (isCurrent)
			{
				refer = base.Sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new WallFilter(), "请选择散热器要布置的位置：");
				result = (base.Doc.GetElement(refer) as Wall);
			}
			else
			{
				refer = base.Sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)5, "请选择需要布置散热器的空间");
				Element elementById = (base.Doc.GetElement(refer) as RevitLinkInstance).GetLinkDocument().GetElementById(refer.LinkedElementId);
				if (elementById is Wall)
				{
					result = (elementById as Wall);
				}
			}
			return result;
		}

		protected Wall SelectWall(ref XYZ selectedPoint, ref Curve wallLocationCurve)
		{
			RevitVersionFuncs.ClearSelection(base.UiDoc.Selection);
			XYZ xyz = new XYZ();
			SelectedElement selectedElement;
			do
			{
				selectedElement = base.UiDoc.PickElementsByPoint("请选择墙面...", 0.05);
			}
			while (!(selectedElement.Element is Wall));
			Wall wall = selectedElement.Element as Wall;
			xyz = selectedElement.SelectedPoint;
			if (wall.Document.IsLinked)
			{
				LinkedDocument linkedDocument = base.UiDoc.Document.GetLinkedDocument(wall.Id);
				selectedPoint = linkedDocument.GetPointInLinkDocument(selectedPoint);
			}
			selectedPoint = xyz;
			wallLocationCurve = this.GetFollowingWallDatumLine(wall, xyz);
			return wall;
		}

		private Curve GetFollowingWallDatumLine(Wall wall, XYZ selectedPoint)
		{
			XYZ xyz = (wall.Location as LocationCurve).Curve.GetEndPoint(0);
			XYZ xyz2 = (wall.Location as LocationCurve).Curve.GetEndPoint(1);
			if (wall.Document.IsLinked)
			{
				LinkedDocument linkedDocument = base.UiDoc.Document.GetLinkedDocument(wall.Id);
				xyz = linkedDocument.GetPointInLinkDocument(xyz);
				xyz2 = linkedDocument.GetPointInLinkDocument(xyz2);
			}
			return Line.CreateBound(xyz, xyz2);
		}

		private void LayRadiator(RadiatorForm rf, LayEquipmentRadiator.RadiatorLayParameter rlp, bool IsSetNum = false, int Num = 0, bool IsDimension = false)
		{
			FamilySymbol familySymbol;
			if (rf.raly.FamilyPath != "")
			{
				familySymbol = YJKRevitTools.GetFamilySymbolByName(rf.raly.FamilyPath, rf.raly.FamilySymbol, base.Doc, BuiltInCategory.OST_MechanicalEquipment);
			}
			else
			{
				familySymbol = rf.Symbol;
			}
			UnifiedModified.ActivateFamilySymbol(familySymbol);
			if (rf.spaceSelected != null && rf.spaceSelected.Document.IsLinked)
			{
				LinkedDocument linkedDocument = base.Doc.GetLinkedDocument(rf.spaceSelected.Id);
				rlp.LocationLay = linkedDocument.GetPointInLinkDocument(rlp.LocationLay);
			}
			FamilyInstance familyInstance = base.Doc.Create.NewFamilyInstance(rlp.LocationLay, familySymbol, (Autodesk.Revit.DB.Structure.StructuralType)0);
			base.Doc.Regenerate();
			Parameter parameter = familyInstance.GetParameter("散热器片数");
			if (IsSetNum && parameter != null)
			{
				parameter.Set(Num);
			}
			base.Doc.Regenerate();
			this.SetRadiatorPara(familyInstance, ref rlp, rf.raly, IsSetNum, Num);
			base.Doc.Regenerate();
			if (rlp.LayType == 1)
			{
				this.RadiatorRotateByOrientation(familyInstance, rlp.WindowFacingOrientation);
			}
			else
			{
				this.MoveRadiator(familyInstance, ref rlp, rf.raly);
			}
			this.SetRadiatorTypeAndVector(rlp.WindowFacingOrientation, rf.raly, familyInstance);
			familyInstance = this.RadiatorRotate(rf.raly, familyInstance, base.Doc);
			if (IsDimension)
			{
				new RadiatorDimension().SetRadiatorDimension(familyInstance, base.Doc, false);
			}
		}

		private XYZ GetRadiatorLayPoint(XYZ pickPoint, Element space, ref XYZ facingOrientation)
		{
			XYZ result = null;
			List<Curve> spaceOrRoomBoundaryCurve = YJKRevitTools.GetSpaceOrRoomBoundaryCurve(space);
			double num = 10000.0;
			XYZ point = (space.Location as LocationPoint).Point;
			foreach (Curve curve in spaceOrRoomBoundaryCurve)
			{
				HYCurve hycurve = new HYCurve(curve);
				if (hycurve.Curve.Distance(pickPoint) < num)
				{
					num = hycurve.Curve.Distance(pickPoint);
					result = hycurve.GetProject(pickPoint, false);
					XYZ project = hycurve.GetProject(point, false);
					facingOrientation = (project - point).Normalize();
				}
			}
			return result;
		}

		private void SetRadiatorPara(FamilyInstance fi, ref LayEquipmentRadiator.RadiatorLayParameter rlp, RadiatorAutoLayPara raly, bool IsSetNum, int Num)
		{
			Parameter parameter = fi.GetParameter("标准散热量");
			Parameter parameter2 = fi.GetParameter("散热器片数");
			Parameter parameter3 = fi.GetParameter("散热器高度");
			Parameter parameter4 = fi.GetParameter("散热器长度");
			Parameter parameter5 = fi.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM);
			rlp.RadiatorLength = ((parameter4 != null) ? parameter4.AsDouble() : 0.0);
			rlp.RadiatorHeight = ((parameter3 != null) ? parameter3.AsDouble() : 0.0);
			if (parameter5 != null)
			{
				parameter5.Set(raly.Offset * 1000.0 / 304.8 + base.Doc.ActiveView.GenLevel.Elevation);
			}
			rlp.UnitHeat = ((parameter != null) ? parameter.AsDouble() : 0.0);
			if (rlp.UnitHeat > 0.001 && rlp.RadiatorHeatLoad > 0.01 && !raly.FamilyPath.Contains("NDRT") && !raly.FamilyPath.Contains("卫浴散热器"))
			{
				int num = (int)Math.Ceiling(rlp.RadiatorHeatLoad / rlp.UnitHeat);
				if (parameter2 != null)
				{
					parameter2.Set(num);
				}
			}
		}

		private void MoveRadiator(FamilyInstance fi, ref LayEquipmentRadiator.RadiatorLayParameter rlp, RadiatorAutoLayPara raly)
		{
			rlp.WindowFacingOrientation = YJKRevitTools.SetZToZero(rlp.WindowLocation - rlp.WindowProject);
			if (rlp.Window.IsCategory(BuiltInCategory.OST_Doors))
			{
				rlp.WindowFacingOrientation = -rlp.WindowFacingOrientation;
			}
			if (rlp.SideWallVector != null)
			{
				rlp.WindowFacingOrientation = rlp.SideWallVector;
			}
			this.RadiatorRotateByOrientation(fi, rlp.WindowFacingOrientation);
			if (rlp.LayType == 0 && rlp.RadiatorHeight + raly.Offset * 1000.0 / 304.8 > rlp.WindowSillHeight)
			{
				rlp.LayType = 4;
			}
			if (rlp.LayType == 4)
			{
				fi.Location.Move((rlp.WindowWidth / 2.0 + 0.328 + rlp.RadiatorLength / 2.0) * rlp.VectorLay);
			}
		}

		private void RadiatorRotateByOrientation(FamilyInstance fi, XYZ windowFacingOrientation)
		{
			if (!windowFacingOrientation.IsAlmostEqualTo(-fi.FacingOrientation))
			{
				double num = fi.FacingOrientation.AngleOnPlaneTo(-windowFacingOrientation, XYZ.BasisZ);
				XYZ point = (fi.Location as LocationPoint).Point;
				YJKLine hyline = new YJKLine(point, point + XYZ.BasisZ);
				fi.Location.Rotate(hyline.Line, num);
			}
		}

		private void GetLayParameter(ref LayEquipmentRadiator.RadiatorLayParameter rlp, RadiatorAutoLayPara raly, Element spaceSelected)
		{
			XYZ xyz = new XYZ();
			Parameter parameter = rlp.Window.GetParameter(BuiltInParameter.INSTANCE_SILL_HEIGHT_PARAM);
			rlp.WindowSillHeight = ((parameter != null) ? parameter.AsDouble() : 0.0);
			if (rlp.Window.IsCategory(BuiltInCategory.OST_Doors))
			{
				rlp.WindowSillHeight = 1000.0;
				rlp.LayType = 3;
			}
			Parameter parameter2 = (rlp.Window as FamilyInstance).Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH);
			if (parameter2 == null)
			{
				parameter2 = rlp.Window.GetParameter("宽度");
			}
			rlp.WindowWidth = ((parameter2 != null) ? parameter2.AsDouble() : 0.0);
			XYZ xyz2 = null;
			rlp.WindowProject = this.GetWindowProject(rlp.Window, spaceSelected, ref xyz, ref xyz2);
			rlp.SideWallVector = xyz2;
			rlp.VectorLay = xyz;
			rlp.WindowLocation = (rlp.Window.Location as LocationPoint).Point;
			Parameter parameter3 = spaceSelected.GetParameter(BuiltInParameter.ROOM_DESIGN_HEATING_LOAD_PARAM);
			rlp.RadiatorHeatLoad = ((parameter3 != null) ? parameter3.AsDouble() : 0.0);
			XYZ xyz3 = (YJKRevitTools.SetZToZero(rlp.WindowProject) - YJKRevitTools.SetZToZero(rlp.WindowLocation)).Normalize() * (raly.WallDistance / 304.8);
			rlp.LocationLay = rlp.WindowProject + xyz3;
			if (xyz2 != null)
			{
				rlp.LayType = 2;
				rlp.LocationLay = rlp.WindowProject + xyz + -xyz2 * (raly.WallDistance / 304.8);
			}
			if (rlp.LayType == 0)
			{
				if (rlp.WindowSillHeight * 304.8 > 600.0)
				{
					rlp.LayType = 0;
				}
				else
				{
					rlp.LayType = 4;
				}
				rlp.LocationLay = rlp.WindowProject + xyz3;
				return;
			}
			if (rlp.LayType == 3)
			{
				rlp.LocationLay = rlp.WindowProject - xyz3;
			}
		}

		private FamilyInstance RadiatorRotate(RadiatorAutoLayPara raly, FamilyInstance radiator, Autodesk.Revit.DB.Document doc)
		{
			Parameter parameter = radiator.GetParameter("散热器宽度");
			Parameter parameter2 = radiator.GetParameter("散热器高度");
			double num = (parameter != null) ? parameter.AsDouble() : 0.0;
			double num2 = (parameter2 != null) ? parameter2.AsDouble() : 0.0;
			XYZ point = (radiator.Location as LocationPoint).Point;
			XYZ facingOrientation = radiator.FacingOrientation;
			XYZ mirrorPnt = point + num / 2.0 * facingOrientation + num2 / 2.0 * XYZ.BasisZ;
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			HYEquipmentCommon hyequipmentCommon = new HYEquipmentCommon();
			ICollection<ElementId> allFamilyInstance = hyequipmentCommon.GetAllFamilyInstance(doc, BuiltInCategory.OST_MechanicalEquipment);
			FamilyInstance result;
			switch (raly.RotateType)
			{
			case 0:
				hyrotateFamilyInstance.MirrorFamilyInstance(radiator, radiator.FacingOrientation, XYZ.BasisZ, mirrorPnt);
				result = this.getMirrorInstance(hyequipmentCommon, allFamilyInstance, doc);
				doc.DeleteElement(radiator);
				break;
			case 1:
				result = radiator;
				break;
			case 2:
				hyrotateFamilyInstance.MirrorFamilyInstance(radiator, XYZ.BasisX, XYZ.BasisY, mirrorPnt);
				result = this.getMirrorInstance(hyequipmentCommon, allFamilyInstance, doc);
				doc.DeleteElement(radiator);
				break;
			default:
				result = radiator;
				break;
			}
			return result;
		}

		private FamilyInstance getMirrorInstance(HYEquipmentCommon equipmentCM, ICollection<ElementId> collectionId, Autodesk.Revit.DB.Document doc)
		{
			FamilyInstance result = null;
			ICollection<ElementId> allFamilyInstance = equipmentCM.GetAllFamilyInstance(doc, BuiltInCategory.OST_MechanicalEquipment);
			new List<ElementId>();
			foreach (ElementId item in collectionId.Intersect(allFamilyInstance))
			{
				allFamilyInstance.Remove(item);
			}
			if (allFamilyInstance != null && allFamilyInstance.Count > 0)
			{
				result = (doc.GetElementById(allFamilyInstance.FirstOrDefault<ElementId>()) as FamilyInstance);
			}
			return result;
		}

		private void SetRadiatorTypeAndVector(XYZ radiatorNormal, RadiatorAutoLayPara raly, FamilyInstance fi)
		{
			XYZ xyz = XYZ.Zero;
			if (raly.InstallType.Equals("12") || raly.InstallType.Equals("34"))
			{
				xyz = radiatorNormal.CrossProduct(XYZ.BasisZ);
			}
			else if (raly.InstallType.Equals("21") || raly.InstallType.Equals("43"))
			{
				xyz = radiatorNormal.CrossProduct(-XYZ.BasisZ);
			}
			YJKRevitTools.SetInstanceParameter(base.UiApp, Path.Combine(Product.DataLocation, "HYShared Parameters.txt"), "散热器布置", "HYRadiatorType", BuiltInCategory.OST_MechanicalEquipment, false);
			Parameter parameter = fi.GetParameter("HYRadiatorType");
			if (parameter != null)
			{
				parameter.Set(raly.InstallType);
			}
			YJKRevitTools.SetInstanceParameter(base.UiApp, Path.Combine(Product.DataLocation, "HYShared Parameters.txt"), "散热器布置", "HYRadiatorVector", BuiltInCategory.OST_MechanicalEquipment, false);
			Parameter parameter2 = fi.GetParameter("HYRadiatorVector");
			if (parameter2 != null)
			{
				parameter2.Set(xyz.Normalize().ToString());
			}
		}

		private XYZ GetWindowProject(Element window, Element space, ref XYZ vectorLay, ref XYZ sideWallVector)
		{
			XYZ result = null;
			Parameter parameter = (window as FamilyInstance).Symbol.GetParameter(BuiltInParameter.CASEWORK_WIDTH);
			if (parameter == null)
			{
				parameter = window.GetParameter("宽度");
			}
			double num = (parameter != null) ? parameter.AsDouble() : 0.0;
			List<Curve> spaceOrRoomBoundaryCurve = YJKRevitTools.GetSpaceOrRoomBoundaryCurve(space);
			XYZ point = (window.Location as LocationPoint).Point;
			double num2 = 1000.0;
			double num3 = 0.0;
			bool flag = false;
			foreach (Curve curve in spaceOrRoomBoundaryCurve)
			{
				HYCurve hycurve = new HYCurve(curve);
				XYZ project = hycurve.GetProject(point, false);
				if (hycurve.PointIsOnCurve(point))
				{
					if (num2 > point.DistanceTo(project) && window.IsCategory(BuiltInCategory.OST_Windows))
					{
						num2 = point.DistanceTo(project);
						result = project;
						if (hycurve.StartPoint.DistanceTo(project) < hycurve.EndPoint.DistanceTo(project))
						{
							if (hycurve.StartPoint.DistanceTo(project) * 304.8 > 500.0 + num * 304.8 / 2.0)
							{
								vectorLay = (hycurve.StartPoint - project).Normalize();
							}
							else if (hycurve.EndPoint.DistanceTo(project) * 304.8 > 500.0 + num * 304.8 / 2.0)
							{
								vectorLay = (hycurve.EndPoint - project).Normalize();
							}
						}
						else if (hycurve.EndPoint.DistanceTo(project) * 304.8 > 500.0 + num * 304.8 / 2.0)
						{
							vectorLay = (hycurve.EndPoint - project).Normalize();
						}
						else if (hycurve.StartPoint.DistanceTo(project) * 304.8 > 500.0 + num * 304.8 / 2.0)
						{
							vectorLay = (hycurve.StartPoint - project).Normalize();
						}
					}
					else if (num3 < point.DistanceTo(project) && window.IsCategory(BuiltInCategory.OST_Doors))
					{
						num3 = point.DistanceTo(project);
						result = project;
					}
				}
			}
			if (flag)
			{
				foreach (Curve curve2 in spaceOrRoomBoundaryCurve)
				{
					HYCurve hycurve2 = new HYCurve(curve2);
					XYZ project2 = hycurve2.GetProject(point, false);
					if (!hycurve2.PointIsOnCurve(point))
					{
						result = project2;
						XYZ xyz = (hycurve2.StartPoint - hycurve2.EndPoint).Normalize();
						XYZ project3 = hycurve2.GetProject(point, true);
						if (hycurve2.EndPoint.IsAlmostEqualTo(project2))
						{
							vectorLay = xyz;
						}
						else
						{
							vectorLay = -xyz;
						}
						sideWallVector = YJKRevitTools.SetZToZero(project3 - point).Normalize();
						break;
					}
				}
			}
			return result;
		}

		private class RadiatorLayParameter
		{
			public Element Window;

			public XYZ WindowLocation = new XYZ();

			public XYZ WindowProject = new XYZ();

			public XYZ WindowFacingOrientation = new XYZ();

			public double RadiatorLength;

			public double RadiatorHeight;

			public double RadiatorHeatLoad;

			public double UnitHeat;

			public XYZ LocationLay = new XYZ();

			public XYZ SideWallVector = new XYZ();

			public XYZ VectorLay = new XYZ();

			public bool IsMiddle;

			public double WindowSillHeight;

			public double WindowWidth;

			public int LayType;
		}
	}
}
